﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using DataAccessEntLib;

public partial class dealer : System.Web.UI.Page
{
    BO.Ad bo = new BO.Ad();
    BLL.AdMagnager bll = new BLL.AdMagnager();
    DAL.AdDB _objAdDB = new DAL.AdDB();
    RegistrationData _objRegistrationData = new RegistrationData();

   public string desc = string.Empty;
 

    protected void BindUserDetails(int AdID)
    {
        if (AdID > 0)
        {
            DataSet ds = new DataSet();
            bo.AdID = AdID;
            ds = bll.SelectAdPostedClientDetails(bo);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow dr=ds.Tables[0].Rows[0];
                    lblName.Text = Convert.ToString(dr["FirstName"]) + " " + Convert.ToString( dr["LastName"]);
                    lblEMail.Text = ds.Tables[0].Rows[0]["Email"].ToString();
                    imgUser.ImageUrl = "/Handler.ashx?ID=" + Convert.ToString(dr["UserId"]);
                    if (Convert.ToBoolean(dr["IsMobileDisclosed"]))
                    {
                        pMobile.Visible = true;
                        lblPhone.Text = Convert.ToString(dr["Mobile"]);
                    }
                    else
                        pMobile.Visible = false;

                }
            }
        }
    }





    protected void Page_Load(object sender, EventArgs e)
    {
        adList1.RequestDataSet += new Classified_UserControls_Cld_List.ReBindDataSet(UC_RequestDataSet);
        if (HttpContext.Current.Request.QueryString["AdID"] != null)
        {
            if (!IsPostBack)
            {
                BindUserDetails(Convert.ToInt32(HttpContext.Current.Request.QueryString["AdID"].ToString()));
                adList1.dsAds = BindAdList(Convert.ToInt32(HttpContext.Current.Request.QueryString["AdID"].ToString()));

                RegistrationData _objRegistrationData = new RegistrationData();
                if (!object.Equals(Session["User"], null))
                {
                    _objRegistrationData = Session["User"] as RegistrationData;

                    txtFName.Text = _objRegistrationData.FirstName +" "+_objRegistrationData.LastName;
                    txtEmail.Text = _objRegistrationData.Email;
                }
                else
                {
                    txtFName.Text = "Your Name";
                    txtEmail.Text = "Your Email";
                }
            }
 
                
        }
        else
        {
            Response.Redirect(Common.LogOutPage);
        }
    }

    DataSet UC_RequestDataSet(object sender)
    {
        return BindAdList(Convert.ToInt32(HttpContext.Current.Request.QueryString["AdID"].ToString()));
    }

    protected DataSet BindAdList(int AdID)
    {
        DataSet ds = new DataSet();
        if (AdID > 0)
        {
            try
            { 
                ds = _objAdDB.Select_AdMaster(AdID,"ContactAd");
             
            }
            catch (Exception ex)
            {
                //lblMessage.Text = ex.Message;
            }
        }
        return ds;
    }


    protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if ((txtFName.Text != string.Empty) && (txtMessage.Text != string.Empty) && (txtEmail.Text != string.Empty))
        {
            if (Validate())
            {
                
                if (!object.Equals(Session["User"], null))
                {
                    _objRegistrationData = Session["User"] as RegistrationData;
                    bo.UserID = _objRegistrationData.UserID;

                }
                string strResult = string.Empty;
                bo.FName = txtFName.Text;
                bo.AdID = Convert.ToInt32(HttpContext.Current.Request.QueryString["AdID"].ToString());
                // bo.LName = txtEmail.Text;
                bo.Email = txtEmail.Text;
                bo.Message = txtMessage.Text;
                strResult = bll.InsertContactDealerDetails(bo);
                if (strResult.Contains("Success"))
                {
                    lblMsg.Text = "Your message has been sent to the Dealer.";
                    SendEmails _objSendEmails = new SendEmails();
                    EmailData.Name = lblName.Text;
                    EmailData.Email = txtEmail.Text;
                    EmailData.From = txtFName.Text;
                    EmailData.Message = txtMessage.Text;
                    _objSendEmails.GeneralEmail("Ad Reply",lblEMail.Text,6);
                    txtFName.Text = string.Empty;
                    txtEmail.Text = string.Empty;
                    txtMessage.Text = string.Empty;
                    BindUserDetails(Convert.ToInt32(HttpContext.Current.Request.QueryString["AdID"].ToString()));
                }
                else
                {
                    lblMsg.Text = strResult;
                }
            }
        }
        else
        {
            lblMsg.Text = "All Fields are required";
        }
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        txtFName.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtMessage.Text = string.Empty;
    }



    public bool Validate()
    {
        RegularExpressionValidator re = new RegularExpressionValidator();
        RegexValidator rv = new RegexValidator();
        if (!rv.IsRegularExpression_Email(txtEmail.Text.Trim()))
        {
            lblMsg.Text = "Please Enter a Valid Email";
            return false;
        }

        return true;
    }

}